function [g_hat] = generate_g_IPW(Y,D,bsperm) 
% This function generates g from the IPW estimator used in the cubic rule
% parfor loop. 

% Input: 
% (1) Y: electricity consumption
% (2) D: RCT treatment assignment
% (3) bsperm: bootstrap permutation index 

% Output 
% (1) g_hat: calculated g at household level
%%
  Yr = Y(bsperm,:);
  Dr= D(bsperm,:);
  psr=mean(Dr);
  g_hat = Yr.*(Dr/psr - (1-Dr)/(1-psr)); % elements of W(G) - W(G_0)
    
end